﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyServer.TcpIP;
using System.Net;
using System.Net.Sockets;
using ScreenMonitor.Common.Hooks;
using MyServer.Udp;
using ScreenMonitor.Common;
using MyServer.Common;
using ScreenMonitor.ClientLib;
using ScreenMonitor.Common.Objects;
using ScreenMonitor.Common.File;
using System.Reflection;

namespace ClientUI
{
    static class Program
    {
        public static MessageWnd MessageWnd = new ClientUI.MessageWnd();
        public static SignWnd SignWnd = new ClientUI.SignWnd();
        public static FileReciveWnd FileReciveWnd = new ClientUI.FileReciveWnd();
        public static ClientComponent ClientComponent = new ClientComponent();
        public static SettingWnd SettingWnd = new SettingWnd();

        private static void Init()
        {
            ClientMain.Init();
            ServerObject.NeedToSign += ServerObject_NeedToSign;
            ServerObject.SendMessage += ServerObject_SendMessage;
            ServerObject.WantToSendFile += ServerObject_WantToSendFile;
        }

        static void ServerObject_WantToSendFile(object sender, OperateObjectEventArgs e)
        {
            if (ServerCommandOperateClass.ShowReciveWndTask.Status != TaskStatus.Running)
            {
                ServerCommandOperateClass.ShowReciveWndTask = new Task(ServerCommandOperateClass.ShowSendFileWnd);
                ServerCommandOperateClass.ShowReciveWndTask.Start();
            }
            Program.FileReciveWnd.AddNewRequest((FileRequestObject)e.OperateObject);
            ServerObject.SendFile((FileRequestObject)e.OperateObject);
        }

        static void ServerObject_SendMessage(object sender, OperateObjectEventArgs e)
        {
            Task tsk = new Task(ServerCommandOperateClass.ShowMessageWnd, e.OperateObject);
            tsk.Start();
        }

        static void ServerObject_NeedToSign(object sender, OperateObjectEventArgs e)
        {
            Task showtsk = new Task(ServerCommandOperateClass.ShowSignWnd, e.OperateObject);
            showtsk.Start();
        }

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {

            Application.EnableVisualStyles();
            try
            {
                Init();
            }
            catch (Exception ex)
            {
                if (MessageBox.Show("客户端初始化失败：" + ex.Message + "\r\n单击确定检查设定后重启程序，点击取消立即退出程序", "程序初始化失败", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
                {
                    SettingWnd.ShowDialog();
                    Application.Restart();
                }
                else
                {
                    return;
                }
            }
            if (!System.IO.File.Exists(Environment.CurrentDirectory + @"\Setting.ini"))
                SettingWnd.ShowDialog();
            ClientMain.Start();
            Application.Run();
        }
    }
}
